package com.richard.myapplication.bounceList;

/**
 * **************************************
 * 项目名称:My Application
 *
 * @Author wuzhiguo
 * 邮箱：wuzhiguo@ksjgs.com
 * 创建时间: 2020/6/16     7:10 PM
 * 用途:
 * **************************************
 */
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ListView;
import android.widget.Toast;

public class BounceListView extends ListView {
    private static final int MAX_Y_OVERSCROLL_DISTANCE = 60;

    private Context mContext;
    private int mMaxYOverscrollDistance;

    private boolean isTouchEvent;
    private float scrollY;

    public BounceListView(Context context) {
        super(context);
        mContext = context;
        initBounceListView();
    }

    public BounceListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initBounceListView();
    }

    public BounceListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        initBounceListView();
    }

    private void initBounceListView() {
        // get the density of the screen and do some maths with it on the max
        // overscroll distance
        // variable so that you get similar behaviors no matter what the screen
        // size

        final DisplayMetrics metrics = mContext.getResources()
                .getDisplayMetrics();
        final float density = metrics.density;
        mMaxYOverscrollDistance = (int) (density * MAX_Y_OVERSCROLL_DISTANCE);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            if (scrollY > mMaxYOverscrollDistance/2 && isTouchEvent) {
                Log.e("自动回弹", "自动回弹==" + scrollY);
                Toast.makeText(getContext(), "自动回弹", Toast.LENGTH_SHORT).show();
            }
        }
        return super.onTouchEvent(ev);
    }

    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX,
                                   int scrollY, int scrollRangeX, int scrollRangeY,
                                   int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
        // This is where the magic happens, we have replaced the incoming
        // maxOverScrollY with our own custom variable mMaxYOverscrollDistance;
        Log.i("dddd",
                "==deltaY==" + deltaY
                + "==scrollY==" + scrollY
                + "==scrollRangeY==" + scrollRangeY
                + "==maxOverScrollY==" + maxOverScrollY
                + "==isTouchEvent==" + isTouchEvent
        );
        this.isTouchEvent = isTouchEvent;
        this.scrollY = scrollY;
        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY,
                scrollRangeX, scrollRangeY, maxOverScrollX,
                mMaxYOverscrollDistance, isTouchEvent);
    }
}